package com.RMP
{
    import flash.display.*;
    import flash.events.*;
    import com.greensock.*;

    public class BookmarksPagesShell extends Sprite
    {
        var startIndexChar:String;
        var myXMLList:XMLList;
        var counter:int;
        var thumbnail:BookmarksPageThumbnail;
        var thumbsArray:Array;
        var nav:BookmarksPaginatedMenu;
        var fadeSpeed:Number;
        var thumbsSprite:Sprite;
        public var hasSub:Boolean;
        var imageOrderString:String = "";
        var myRoot:MovieClip;
        var currNavId:int;
        var startIndexNum:int;

        public function BookmarksPagesShell(param1:MovieClip, param2:Number, param3:XMLList) : void
        {
            var _loc_4:* = undefined;
            imageOrderString = "";
            visible = false;
            alpha = 0;
            myRoot = param1;
            fadeSpeed = param2;
            myXMLList = param3;
            startIndexNum = 0;
            currNavId = 0;
            _loc_4 = Math.ceil(myXMLList.length() / 6);
            nav = new BookmarksPaginatedMenu(myRoot, fadeSpeed, _loc_4);
            nav.addEventListener(PaginatedMenuClick.MENU_CLICK, menuClick, false, 0, true);
            addChild(nav);
            buildGrid();
            updateGrid();
            TweenMax.to(thumbsSprite, fadeSpeed, {alpha:1, overwrite:true});
            nav.switchCols(currNavId);
            myRoot.stage.addEventListener(Event.RESIZE, onResizer);
            doResize();
            return;
        }

        public function fadeOutShell() : void
        {
            TweenMax.to(this, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                visible = false;
                return;
            }
            });
            return;
        }

        function doResize() : void
        {
            var _loc_1:int = 0;
            nav.x = Math.round(10 * (myRoot.unitW + myRoot.gutterW));
            nav.y = Math.round(myRoot.headerH + (myRoot.unitH + myRoot.gutterH) + -5 * myRoot.unitW / 75);
            if (thumbsArray != null)
            {
                _loc_1 = 0;
                while (_loc_1 < thumbsArray.length)
                {
                    
                    thumbsArray[_loc_1].x = Math.round(_loc_1 % 3 * 4 * (myRoot.unitW + myRoot.gutterW));
                    thumbsArray[_loc_1].y = Math.round(myRoot.headerH + 2 * (myRoot.unitH + myRoot.gutterH) + Math.floor(_loc_1 % 6 / 3) * 2 * (myRoot.unitH + myRoot.gutterH));
                    _loc_1++;
                }
            }
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        function menuClick(param1:PaginatedMenuClick)
        {
            var _loc_2:* = param1.currentTarget.currNavId;
            currNavId = param1.currentTarget.currNavId;
            startIndexNum = _loc_2;
            updateGrid();
            return;
        }

        function btnRollOver(event:MouseEvent)
        {
            event.currentTarget.rollOverEvent();
            return;
        }

        function updateGridNoFade() : void
        {
            var _loc_1:int = 0;
            imageOrderString = "";
            _loc_1 = 0;
            while (_loc_1 < thumbsArray.length)
            {
                
                thumbsArray[_loc_1].idNum = _loc_1;
                if (_loc_1 != (thumbsArray.length - 1))
                {
                    imageOrderString = imageOrderString + (thumbsArray[_loc_1].dbId + ",");
                }
                else
                {
                    imageOrderString = imageOrderString + thumbsArray[_loc_1].dbId;
                }
                if (_loc_1 < currNavId * 6 || _loc_1 >= 6 + currNavId * 6)
                {
                    thumbsArray[_loc_1].visible = false;
                    thumbsArray[_loc_1].alpha = 0;
                }
                else
                {
                    thumbsArray[_loc_1].visible = true;
                    thumbsArray[_loc_1].alpha = 1;
                }
                _loc_1++;
            }
            doResize();
            return;
        }

        function deleteThumbnail(param1, param2) : void
        {
            var _loc_3:int = 0;
            var _loc_4:* = undefined;
            myRoot.removeFromBookmarks(param2, null);
            _loc_3 = 0;
            while (_loc_3 < thumbsArray.length)
            {
                
                if (thumbsArray[_loc_3].idNum == param1)
                {
                    thumbsArray[_loc_3].removeEventListener(MouseEvent.CLICK, btnRollOver);
                    thumbsArray[_loc_3].removeEventListener(MouseEvent.ROLL_OVER, btnRollOut);
                    thumbsArray[_loc_3].removeEventListener(MouseEvent.ROLL_OUT, btnClick);
                    thumbsSprite.removeChild(thumbsArray[_loc_3]);
                    thumbsArray[_loc_3] = null;
                    thumbsArray.splice(_loc_3, 1);
                    break;
                }
                _loc_3++;
            }
            dispatchEvent(new BookmarksDeleteEvent(thumbsArray.length, null));
            removeChild(nav);
            nav.removeEventListener(PaginatedMenuClick.MENU_CLICK, menuClick);
            nav = null;
            _loc_4 = Math.ceil(thumbsArray.length / 6);
            nav = new BookmarksPaginatedMenu(myRoot, fadeSpeed, _loc_4);
            nav.addEventListener(PaginatedMenuClick.MENU_CLICK, menuClick, false, 0, true);
            if (currNavId > (nav.btnsArray.length - 1))
            {
                currNavId = nav.btnsArray.length - 1;
            }
            nav.switchCols(currNavId);
            addChild(nav);
            updateGridNoFade();
            return;
        }

        function btnRollOut(event:MouseEvent)
        {
            event.currentTarget.rollOutEvent();
            return;
        }

        function updateGrid() : void
        {
            var _loc_1:int = 0;
            imageOrderString = "";
            _loc_1 = 0;
            while (_loc_1 < thumbsArray.length)
            {
                
                thumbsArray[_loc_1].idNum = _loc_1;
                if (_loc_1 != (thumbsArray.length - 1))
                {
                    imageOrderString = imageOrderString + (thumbsArray[_loc_1].dbId + ",");
                }
                else
                {
                    imageOrderString = imageOrderString + thumbsArray[_loc_1].dbId;
                }
                if (_loc_1 < currNavId * 6 || _loc_1 >= 6 + currNavId * 6)
                {
                    thumbsArray[_loc_1].fadeOutShell();
                }
                else
                {
                    thumbsArray[_loc_1].fadeInShell();
                }
                _loc_1++;
            }
            doResize();
            return;
        }

        function shiftThumbnailLeft(param1, param2) : void
        {
            var _loc_3:* = undefined;
            _loc_3 = thumbsArray[param1];
            thumbsArray.splice(param1, 1);
            thumbsArray.splice((param1 - 1), 0, _loc_3);
            updateGridNoFade();
            myRoot.reorderBookmarks("bookmarks", imageOrderString);
            return;
        }

        function shiftThumbnailRight(param1, param2) : void
        {
            var _loc_3:* = undefined;
            _loc_3 = thumbsArray[param1];
            thumbsArray.splice(param1, 1);
            thumbsArray.splice((param1 + 1), 0, _loc_3);
            updateGridNoFade();
            myRoot.reorderBookmarks("bookmarks", imageOrderString);
            return;
        }

        function btnClick(event:MouseEvent) : void
        {
            if (event.currentTarget.icons.hover == false)
            {
                updateNewsAddress(myXMLList[event.currentTarget.idNum].@type, myXMLList[event.currentTarget.idNum].@id);
            }
            else if (event.currentTarget.icons.hoverMinus == true)
            {
                deleteThumbnail(event.currentTarget.idNum, event.currentTarget.dbId);
            }
            else if (event.currentTarget.icons.hoverLeft == true)
            {
                if (thumbsArray[0].idNum != event.currentTarget.idNum)
                {
                    shiftThumbnailLeft(event.currentTarget.idNum, event.currentTarget.dbId);
                }
            }
            else if (event.currentTarget.icons.hoverRight == true)
            {
                if (thumbsArray[(thumbsArray.length - 1)].idNum != event.currentTarget.idNum)
                {
                    shiftThumbnailRight(event.currentTarget.idNum, event.currentTarget.dbId);
                }
            }
            return;
        }

        function buildGrid() : void
        {
            var _loc_1:int = 0;
            var _loc_2:String = null;
            var _loc_3:String = null;
            thumbsSprite = new Sprite();
            thumbsSprite.alpha = 0;
            addChild(thumbsSprite);
            thumbsArray = new Array();
            _loc_1 = 0;
            while (_loc_1 < myXMLList.length())
            {
                
                if (myXMLList[_loc_1].@type == "New York Staff" || myXMLList[_loc_1].@type == "Los Angeles Staff" || myXMLList[_loc_1].@type == "Partner")
                {
                    _loc_2 = myXMLList[_loc_1].@type + "<br><span class = \'myBold\'>" + myXMLList[_loc_1].shortTitle + "</span>";
                }
                else if (myXMLList[_loc_1].@type == "Press Publications" || myXMLList[_loc_1].@type == "Press Releases")
                {
                    _loc_2 = myXMLList[_loc_1].@type + "<br>" + myXMLList[_loc_1].longTitle;
                }
                else if (myXMLList[_loc_1].date.@season != undefined)
                {
                    if (myXMLList[_loc_1].date.@season == " " || myXMLList[_loc_1].date.@season == "")
                    {
                        _loc_2 = myXMLList[_loc_1].@type + "<br><span class = \'myBold\'>" + myXMLList[_loc_1].shortTitle + "</span><br>" + myXMLList[_loc_1].date.@year;
                    }
                    else
                    {
                        _loc_2 = myXMLList[_loc_1].@type + "<br><span class = \'myBold\'>" + myXMLList[_loc_1].shortTitle + "</span><br>" + myXMLList[_loc_1].date.@season + " " + myXMLList[_loc_1].date.@year;
                    }
                }
                else if (myXMLList[_loc_1].date.@year == "-1")
                {
                    _loc_2 = myXMLList[_loc_1].@type + "<br><span class = \'myBold\'>" + myXMLList[_loc_1].shortTitle + "</span><br>" + "Current";
                }
                else if (myXMLList[_loc_1].date.@year == "-100")
                {
                    _loc_2 = myXMLList[_loc_1].@type + "<br><span class = \'myBold\'>" + myXMLList[_loc_1].shortTitle + "</span><br>" + "Unbuilt";
                }
                else
                {
                    _loc_2 = myXMLList[_loc_1].@type + "<br><span class = \'myBold\'>" + myXMLList[_loc_1].shortTitle + "</span><br>" + myXMLList[_loc_1].date.@year;
                }
                _loc_3 = myXMLList[_loc_1].thumbnail.@border;
                if (_loc_3 == "")
                {
                    _loc_3 = "0";
                }
                if (myXMLList[_loc_1].thumbnail.@url != "")
                {
                    thumbnail = new BookmarksPageThumbnail(myRoot, fadeSpeed, _loc_2, _loc_1, myXMLList[_loc_1].@id, myXMLList[_loc_1].thumbnail.@url, _loc_3);
                }
                else
                {
                    thumbnail = new BookmarksPageThumbnail(myRoot, fadeSpeed, _loc_2, _loc_1, myXMLList[_loc_1].@id, myXMLList[_loc_1].thumbnail.@url, _loc_3);
                }
                thumbnail.visible = false;
                thumbsSprite.addChild(thumbnail);
                thumbnail.buttonMode = true;
                thumbnail.mouseChildren = true;
                thumbnail.addEventListener(MouseEvent.MOUSE_OVER, btnRollOver);
                thumbnail.addEventListener(MouseEvent.MOUSE_OUT, btnRollOut);
                thumbnail.addEventListener(MouseEvent.CLICK, btnClick);
                thumbsArray.push(thumbnail);
                _loc_1++;
            }
            return;
        }

        function updateNewsAddress(param1:String, param2:int) : void
        {
            var incomingType:* = param1;
            var incomingId:* = param2;
            trace("INCOMING TYPE " + incomingType);
            switch(incomingType)
            {
                case "Awards":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "awards" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Partners":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "partners" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "New York Staff":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "staff" + "/" + "new-york" + "/" + "senior-staff" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Los Angeles Staff":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "staff" + "/" + "los-angeles" + "/" + "senior-staff" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Press Publications":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "press" + "/" + "publications" + "/" + "archive" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Press Releases":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "press" + "/" + "releases" + "/" + "archive" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Lectures":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "lectures" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Books":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "books" + "/" + "archive" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Selected Text":
                {
                    myRoot.clickPathNames = [""];
                    myRoot.updateAddress("/" + "practice" + "/" + "selected-text" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Press Video":
                {
                    myRoot.updateAddress("/" + "practice" + "/" + "press" + "/" + "video" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    break;
                }
                case "Architecture":
                {
                    var _loc_5:int = 0;
                    var _loc_8:int = 0;
                    var _loc_11:int = 0;
                    var _loc_12:* = myRoot.myXML.nav.projects.tier0;
                    var _loc_10:* = new XMLList("");
                    for each (_loc_13 in _loc_12)
                    {
                        
                        var _loc_14:* = _loc_12[_loc_11];
                        with (_loc_12[_loc_11])
                        {
                            if (@label == "Architecture")
                            {
                                _loc_10[_loc_11] = _loc_13;
                            }
                        }
                    }
                    var _loc_9:* = _loc_10.tier1;
                    var _loc_7:* = new XMLList("");
                    for each (_loc_10 in _loc_9)
                    {
                        
                        var _loc_11:* = _loc_9[_loc_8];
                        with (_loc_9[_loc_8])
                        {
                            if (@label == "Name")
                            {
                                _loc_7[_loc_8] = _loc_10;
                            }
                        }
                    }
                    var _loc_6:* = _loc_7.tier2.item;
                    var _loc_4:* = new XMLList("");
                    for each (_loc_7 in _loc_6)
                    {
                        
                        var _loc_8:* = _loc_6[_loc_5];
                        with (_loc_6[_loc_5])
                        {
                            if (@id == incomingId)
                            {
                                _loc_4[_loc_5] = _loc_7;
                            }
                        }
                    }
                    if (_loc_4.@sub == "T")
                    {
                        myRoot.clickPathNames = [""];
                        myRoot.updateAddress("/" + "projects" + "/" + "architecture" + "/" + "name" + "/" + 0 + "/" + incomingId + "/" + "sub" + "/" + 0 + "/" + 0 + "/");
                    }
                    else
                    {
                        myRoot.clickPathNames = [""];
                        myRoot.updateAddress("/" + "projects" + "/" + "architecture" + "/" + "name" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    }
                    break;
                }
                case "Products":
                {
                    var _loc_5:int = 0;
                    var _loc_8:int = 0;
                    var _loc_11:int = 0;
                    var _loc_12:* = myRoot.myXML.nav.projects.tier0;
                    var _loc_10:* = new XMLList("");
                    for each (_loc_13 in _loc_12)
                    {
                        
                        var _loc_14:* = _loc_12[_loc_11];
                        with (_loc_12[_loc_11])
                        {
                            if (@label == "Products")
                            {
                                _loc_10[_loc_11] = _loc_13;
                            }
                        }
                    }
                    var _loc_9:* = _loc_10.tier1;
                    var _loc_7:* = new XMLList("");
                    for each (_loc_10 in _loc_9)
                    {
                        
                        var _loc_11:* = _loc_9[_loc_8];
                        with (_loc_9[_loc_8])
                        {
                            if (@label == "Name")
                            {
                                _loc_7[_loc_8] = _loc_10;
                            }
                        }
                    }
                    var _loc_6:* = _loc_7.tier2.item;
                    var _loc_4:* = new XMLList("");
                    for each (_loc_7 in _loc_6)
                    {
                        
                        var _loc_8:* = _loc_6[_loc_5];
                        with (_loc_6[_loc_5])
                        {
                            if (@id == incomingId)
                            {
                                _loc_4[_loc_5] = _loc_7;
                            }
                        }
                    }
                    if (_loc_4.@sub == "T")
                    {
                        myRoot.clickPathNames = [""];
                        myRoot.updateAddress("/" + "projects" + "/" + "products" + "/" + "name" + "/" + 0 + "/" + incomingId + "/" + "sub" + "/" + 0 + "/" + 0 + "/");
                    }
                    else
                    {
                        myRoot.clickPathNames = [""];
                        myRoot.updateAddress("/" + "projects" + "/" + "products" + "/" + "name" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    }
                    break;
                }
                case "Exhibitions":
                {
                    var _loc_5:int = 0;
                    var _loc_8:int = 0;
                    var _loc_11:int = 0;
                    var _loc_12:* = myRoot.myXML.nav.projects.tier0;
                    var _loc_10:* = new XMLList("");
                    for each (_loc_13 in _loc_12)
                    {
                        
                        var _loc_14:* = _loc_12[_loc_11];
                        with (_loc_12[_loc_11])
                        {
                            if (@label == "Exhibitions")
                            {
                                _loc_10[_loc_11] = _loc_13;
                            }
                        }
                    }
                    var _loc_9:* = _loc_10.tier1;
                    var _loc_7:* = new XMLList("");
                    for each (_loc_10 in _loc_9)
                    {
                        
                        var _loc_11:* = _loc_9[_loc_8];
                        with (_loc_9[_loc_8])
                        {
                            if (@label == "Name")
                            {
                                _loc_7[_loc_8] = _loc_10;
                            }
                        }
                    }
                    var _loc_6:* = _loc_7.tier2.item;
                    var _loc_4:* = new XMLList("");
                    for each (_loc_7 in _loc_6)
                    {
                        
                        var _loc_8:* = _loc_6[_loc_5];
                        with (_loc_6[_loc_5])
                        {
                            if (@id == incomingId)
                            {
                                _loc_4[_loc_5] = _loc_7;
                            }
                        }
                    }
                    if (_loc_4.@sub == "T")
                    {
                        myRoot.clickPathNames = [""];
                        myRoot.updateAddress("/" + "projects" + "/" + "exhibitions" + "/" + "name" + "/" + 0 + "/" + incomingId + "/" + "sub" + "/" + 0 + "/" + 0 + "/");
                    }
                    else
                    {
                        myRoot.clickPathNames = [""];
                        myRoot.updateAddress("/" + "projects" + "/" + "exhibitions" + "/" + "name" + "/" + 0 + "/" + incomingId + "/" + 0 + "/");
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }

        public function fadeInShell() : void
        {
            visible = true;
            TweenMax.to(this, fadeSpeed, {alpha:1, delay:fadeSpeed, overwrite:true});
            return;
        }

    }
}
